package com.xianniu.ltc.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.api.RespBody;
import com.rebuild.core.UserContextHolder;
import com.xianniu.ltc.enums.OperatingStateEnum;
import com.xianniu.ltc.feign.api.ThirdPartyService;
import com.xianniu.ltc.model.po.QccFuzzySearchPO;
import com.xxl.sso.core.entity.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;

/**
 * @author WangRuiJie
 * @Title: GetEnterpriseUtil
 * @Package com.xianniu.ltc.util
 * @Description: 获取企业信息工具类
 * @date 2022/3/2410:54 上午
 */
@Slf4j
@Component
public class GetEnterpriseUtil {

    @Autowired
    private ThirdPartyService thirdPartyService;

    /**
     * 调用三方根据企业名称查询复符合条件的企业信息
     * @param po
     * @return
     */
    public JSONAware fuzzySearch(QccFuzzySearchPO po){
        ReturnT returnT = thirdPartyService.fuzzySearch(po, UserContextHolder.getToken());
        log.info("查询企业返回信息：{}", JSON.toJSON(returnT));
        if (returnT.getCode() != 200){
          return RespBody.errorl(returnT.getMsg());
        }
        JSONArray jsonArray = new JSONArray();
        List<Map> dataList = (List<Map>) returnT.getData();
        for (Map data : dataList){
            JSONObject vo = new JSONObject();
            // 企业名称
            vo.put("qiyexinxiName", data.get("Name"));
            // 统一信用代码
            vo.put("tongyishehuixinyongdaima", data.get("CreditCode"));
            // 法人姓名
            vo.put("farenxingming", data.get("OperName"));
            // 经营状态
            Object state = data.get("Status");
            if (OperatingStateEnum.getByValue(state.toString()) == null){
                vo.put("jingyingzhuangtai", null);
            }else {
                JSONObject j = new JSONObject();
                j.put("id", OperatingStateEnum.getByValue(state.toString()).getKey());
                j.put("text", state.toString());
                vo.put("jingyingzhuangtai", j);
            }
            jsonArray.add(vo);
            log.info("返回信息：{}", data);
        }
        return jsonArray;
    }

}
